/**
 * Created by yuan_rz on 2018/04/13.
 */
define(function () {

    var self, content, orderItem, userInfo, fwpg;

    function FwpgInfo() {
        self = this;
        self.vue = null;
        self.mData = {
            orderList: [],
            date: null
        };
        fwpg = app.pageParameters['fwzp-info'];
        self.mData.orderList = fwpg.items;
        for (var i = 0; i < self.mData.orderList.length; i++) {
            if (self.mData.date == null) {
                self.mData.date = self.mData.orderList[i].MMRD024;
            } else {
                if (new Date() - Date.parse(self.mData.orderList[i].MMRD024) > 0) {
                    self.mData.date = Date.parse(self.mData.orderList[i].MMRD024);
                }
            }
        }
        self.init();
    }

    //初始化Vue
    FwpgInfo.prototype.initVue = function () {
        var self = this;
        self.vue = new Vue({
            el: doc.getElementById("app"),
            data: function () {
                return {
                    list: [],
                    date: self.mData.date,
                    userId: '',
                    orderList:self.mData.orderList
                }
            },
            methods: {
                submit: function () {

                    var _xdata = {};
                    _xdata.orderList = JSON.stringify(self.mData.orderList);
                    _xdata.userId = self.vue.userId;
                    if (_xdata.userId == '' || _xdata.userId == null) {
                        app.toast('请选择维修人员');
                        return;
                    }

                    _xdata.date = self.vue.date;
                    if (_xdata.date == undefined){
                        var date = new Date();
                        var year = date.getFullYear();
                        var month = date.getMonth() + 1;
                        var strDate = date.getDate();
                        if (month >= 1 && month <= 9) {
                            month = "0" + month;
                        }
                        if (strDate >= 0 && strDate <= 9) {
                            strDate = "0" + strDate;
                        }
                        _xdata.date = year + '-' + month + '-' + strDate;
                    }

                    for (var i = 0; i < this.list.length; i++) {
                        if (_xdata.userId == this.list[i].ID) {
                            _xdata.MMRE004 = this.list[i].MMRE004;
                            _xdata.orgId = this.list[i].ORG_ID;
                        }
                    }
                    app.confirm({
                        msg: '确定派工?',
                        ok: function () {

                            app.ajax({
                                xurl: "api/wx/fwzp/saveFwpg",
                                loading: true,
                                xdata: _xdata,
                                cb: function (result) {
                                    if (result.ok) {
                                        app.alert({
                                            msg: '派工成功！', ok: function () {
                                                app.back(fwpg.backNum || 1);
                                            }
                                        });
                                        fwpg.cb();
                                    } else {
                                        app.toast(result.data.appmsg);
                                    }
                                }
                            });
                        }
                    });
                },
                selectDate:function (index,v) {
                    self.vue.date = v;
                }
            },
            created: function () {
                var vue = this;
                app.ajax({
                    xurl: "api/wx/fwpg/getUserListData",
                    xdata: {
                        MMRD006: fwpg.items[0].MMRD006,
                        MMRD007: fwpg.items[0].MMRD007
                    },
                    cb: function (result) {
                        if (result.ok) {
                            vue.list = result.data.data;
                        }
                    }
                });
            }
        });
    };

    FwpgInfo.prototype.init = function () {
        self.initVue();
        mui('.mui-scroll-wrapper').scroll({
            deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
        });
    };

    return new FwpgInfo();

});